package com.kh.rationaldrugreview.dto;

import com.kh.rationaldrugreview.enums.RiskLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

import java.util.List;

/**
 * 临床建议
 * Clinical Recommendation
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ClinicalRecommendation {
    
    /**
     * 建议ID
     */
    private String recommendationId;
    
    /**
     * 建议类型
     */
    private RecommendationType type;
    
    /**
     * 优先级
     */
    private RiskLevel priority;
    
    /**
     * 建议标题
     */
    private String title;
    
    /**
     * 建议内容
     */
    private String content;
    
    /**
     * 具体行动建议
     */
    private List<String> actions;
    
    /**
     * 涉及的药物
     */
    private List<String> affectedDrugs;
    
    /**
     * 替代药物建议
     */
    private List<AlternativeDrug> alternativeDrugs;
    
    /**
     * 剂量调整建议
     */
    private DosageAdjustment dosageAdjustment;
    
    /**
     * 监测建议
     */
    private MonitoringRecommendation monitoring;
    
    /**
     * 理由/依据
     */
    private String rationale;
    
    /**
     * 证据等级
     */
    private String evidenceLevel;
    
    /**
     * 建议来源
     */
    private String source;
    
    /**
     * 是否强制执行
     */
    private Boolean mandatory;
    
    /**
     * 建议类型枚举
     */
    public enum RecommendationType {
        STOP_MEDICATION("停止用药"),
        REPLACE_MEDICATION("更换药物"),
        ADJUST_DOSE("调整剂量"),
        ADD_MONITORING("加强监测"),
        ADD_PRECAUTION("增加注意事项"),
        PATIENT_COUNSELING("患者教育"),
        CLINICAL_CONSULTATION("临床会诊");
        
        private final String description;
        
        RecommendationType(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    /**
     * 替代药物
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class AlternativeDrug {
        private String drugName;
        private String genericName;
        private String reason;
        private String dosage;
        private RiskLevel riskLevel;
        private String note;
    }
    
    /**
     * 剂量调整建议
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class DosageAdjustment {
        private String currentDose;
        private String recommendedDose;
        private String adjustmentRatio;
        private String adjustmentFormula;
        private String reason;
        private String timing;
        private List<String> precautions;
    }
    
    /**
     * 监测建议
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class MonitoringRecommendation {
        private List<String> parameters;
        private String frequency;
        private String duration;
        private List<String> warningValues;
        private String actionOnAbnormal;
        private String note;
    }
    
    /**
     * 获取建议的紧急程度
     */
    public int getUrgency() {
        return priority != null ? priority.getLevel() : 0;
    }
    
    /**
     * 判断是否为强制性建议
     */
    public boolean isMandatory() {
        return Boolean.TRUE.equals(mandatory) || 
               priority == RiskLevel.CRITICAL;
    }
    
    /**
     * 生成建议摘要
     */
    public String generateSummary() {
        StringBuilder summary = new StringBuilder();
        
        if (type != null) {
            summary.append(type.getDescription()).append("：");
        }
        
        if (title != null) {
            summary.append(title);
        } else if (content != null) {
            summary.append(content.length() > 50 ? 
                content.substring(0, 50) + "..." : content);
        }
        
        if (affectedDrugs != null && !affectedDrugs.isEmpty()) {
            summary.append(" (涉及药物：")
                   .append(String.join("、", affectedDrugs))
                   .append(")");
        }
        
        return summary.toString();
    }
}